package async;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.protobuf.Message;

public class ManagerImpl implements Manager {

	private final static Logger log = LoggerFactory.getLogger(ManagerImpl.class);
	
	private NonBlockingConnector connector;
	private NonBlockingSender sender;
	private MessageSerializer serializer;
	
	public Connection connect(final String hostname, final int port) {
		return new Connection() {
			public void send(Message message) {
				try {
					SocketChannel socketChannel = connector.connect(hostname, port);
					log.debug("Channel: {}", socketChannel);
					ByteBuffer buffer = serializer.serilize(message);
					log.debug("Buffer: {}", buffer);
					sender.send(socketChannel, buffer);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		};
	}
	
	
	
}
